package com.neu.wms.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.wms.dto.CustomerDto;

import com.neu.wms.entity.WmsCustomer;
import com.neu.wms.service.CustomerService;
import com.neu.wms.service.impl.CustomerServiceImpl;
import com.neu.wms.util.ResultVo;
import com.neu.wms.vo.CustomerVo;
import com.neu.wms.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.EmptyStackException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @PostMapping("queryAll")
    public ResultVo selectAll(@RequestBody CustomerDto customerDto)
    {
        PageHelper.startPage(customerDto.getCurrentPage(),customerDto.getPageSize(),true);
        List<CustomerVo> customer = customerService.selectAll(customerDto);
        PageInfo<CustomerVo> pageInfo = new PageInfo<>(customer);
        return ResultVo.ok().data("list",customer).data("total",pageInfo.getTotal());
    }

    @GetMapping("{id}")
    public ResultVo queryById(@PathVariable("id") Long id){
        WmsCustomer wmsCustomer = customerService.selectByPrimaryKey(id);
        return ResultVo.ok().data("customer",wmsCustomer);
    }

    @PostMapping("")
    public ResultVo add(@RequestBody @Valid WmsCustomer customer)
    {
        int i = customerService.insert(customer);
        if (i>0) {
            return ResultVo.ok("添加成功");
        }else {
            return ResultVo.error("添加失败");
        }
    }

    @PutMapping("")
    public ResultVo update(@RequestBody @Valid WmsCustomer wmsCustomer)
    {
        int i = customerService.update(wmsCustomer);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @PutMapping("/quit")
    public ResultVo updateDelFlag(@RequestBody CustomerDto customerDto)
    {
        if(customerDto.getDelFlag()==null)
            throw new IllegalArgumentException("Argument cannot be null or empty");
        int i = customerService.updateDelFlag(customerDto);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @DeleteMapping("{id}")
    public ResultVo delete(@PathVariable("id")Long id)
    {
        int i = customerService.delete(id);
        if (i>0) {
            return ResultVo.ok("删除成功");
        }else {
            return ResultVo.error("删除失败");
        }
    }
}
